﻿
    <?php

     function thumb($filePath,$filename){
        //原图路径
        $imgPath = $filePath.$filename;
        //缩率图路径1 ﻿ /uploads/2018-12/28/thumb/20181228134338339412.jpeg
        //缩率图路径2 ﻿ /uploads/2018-12/28/thumb_20181228134338339412.jpeg
        // echo $imgPath;die;
        //获取你要生成缩率图的原图的大小
        $old = getimagesize($imgPath);

        /*
          getimagesize函数返回的数据如下
  		Array
  		(
  		    [0] => 500  宽度
  		    [1] => 500  高度
  		    [2] => 2    图片类型
  		    [3] => width="500" height="500"
  		    [bits] => 8
  		    [channels] => 3
  		    [mime] => image/jpeg
  		)
         */
        
        //计算缩率图的缩放比例
        $thumb_w = $old[0]/2;
        $thumb_h = $old[1]/2;

        //把原图转成图片资源格式类型
        switch ($old[2]) {
        	case '1':
        		$old_img = imagecreatefromgif($imgPath);
        		break;      	
          case '2':
        		$old_img = imagecreatefromjpeg($imgPath);
        		break;
        	default:
        		$old_img = imagecreatefrompng($imgPath);
        		break;
        }
        
        //画缩率图大小的图像
        $thumb = imagecreatetruecolor($thumb_w,$thumb_h);

        //将原图缩放成缩率图
        imagecopyresized  ( $thumb , $old_img , 0 , 0 , 0 , 0 , $thumb_w , $thumb_h , $old[0] , $old[1] );

        //组装缩略图的路径
        $thumbPath = $filePath.'thumb_'.$filename;

        ob_end_clean(); 
      //根据原图类型生成对应类型的缩率图
        switch ($old[2]) {
        	case '1':
        		$old_img = imagegif($thumb,$thumbPath);
        		break;      	
          case '2':
        		$old_img = imagejpeg($thumb,$thumbPath);
        		break;
        	default:
        		$old_img = imagepng($thumb,$thumbPath);
        		break;
        }

        //销毁缩率图
        imagedestroy($thumb);

        return $thumbPath;
}